package com.loto.insurance.oem.web.entity;

import java.text.MessageFormat;

public class Pager {
	private int index = 1;
	private int pageSize = 10;
	private long rowCount;
	private String params = "";
	private String pageName;
	private int pageCount;
	private String pager;
	private int pageNum = 5;

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	public int getPageCount() {
		if (rowCount > 0 && pageSize > 0)
			return (int) Math.ceil((double) rowCount / (double) pageSize);
		else
			return 0;
	}

	public String getPager() {
		String prePage = "上一页";
		String nextPage = "下一页";
		String firstPage = "第一页";
		String lastPage = "最后一页";
		String formatPage = "<a href=\"{0}\">{1}</a>";
		String formatUrl = "{0}?page={1}{2}";
		String formatCurrentPage = "<span>{0}</span>";
		StringBuilder builder = new StringBuilder();
		pageCount = getPageCount();
		if (params != null && !params.isEmpty() && !params.startsWith("&"))
			params = "&" + params;
		int midpage = ((index - 1) / pageNum);
		int pageoffset = (index > pageNum / 2) ? (index - 1 - pageNum / 2)
				: (midpage * pageNum);
		int endpage = ((pageoffset + pageNum) > pageCount) ? pageCount
				: (pageoffset + pageNum);
		pageoffset = (index > pageNum && endpage == pageCount) ? (endpage - pageNum)
				: pageoffset;

		if (index > 1) {
			builder.append(MessageFormat.format(formatPage,
					MessageFormat.format(formatUrl, pageName, 1, params),
					firstPage));
			builder.append(MessageFormat.format(formatPage, MessageFormat
					.format(formatUrl, pageName, index - 1, params), prePage));
		}

		for (int i = pageoffset + 1; i <= endpage; i++) {
			if (i != index) {
				builder.append(MessageFormat.format(formatPage,
						MessageFormat.format(formatUrl, pageName, i, params), i));
			} else {
				builder.append(MessageFormat.format(formatCurrentPage, i));
			}
		}

		if (index < pageCount) {
			builder.append(MessageFormat.format(formatPage, MessageFormat
					.format(formatUrl, pageName, index + 1, params), nextPage));
			builder.append(MessageFormat.format(formatPage, MessageFormat
					.format(formatUrl, pageName, pageCount, params), lastPage));
		}
		return builder.toString();
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public void setPager(String pager) {
		this.pager = pager;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

}
